home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2860 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.4 KB

  1. Path: news.iag.net!news
  2. From: jatmon@iag.net (John R Buchan)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: why get 9 strings only?
  5. Date: 24 Jan 1996 15:53:40 GMT
  6. Organization: Internet Access Group, Orlando, Florida
  7. Message-ID: <4e5km4$2e6@news.iag.net>
  8. References: <4e51th$4bi@news.nevada.edu>
  9. NNTP-Posting-Host: pm2-orl16.iag.net
  10. X-Newsreader: WinVN 0.99.7
  11.  
  12. In article <4e51th$4bi@news.nevada.edu>, chancl@nevada.edu says...
  13. >
  14. >I can't figure out why I can only input 9 strings
  15. >instead of 10 on the following program:
  16. >
  17. >**************************************************************
  18. >
  19. >/* Write a program that reads a number and then a list of  */
  20. >/* strings (all on separate lines), and then prints one of */ 
  21. >/* the lines from the list as selected by the number.      */
  22. >
  23. >#include <stdio.h>
  24. >
  25. >void main()
  26.  
  27. In anci c code, main must return int. A return of void is undefined.
  28.  
  29. >{
  30. >        char str[10][80];
  31. >        int i, j;
  32. >
  33. >        printf("Enter a number (0-9):\n");
  34. >        scanf("%d", &j);
  35.  
  36. Using scanf for input to input numeric values is tricky.  Here you've
  37. told it to read an integer value and store it in j.  It obediently
  38. reads chars until it reaches the first one that isn't allowed for ints.
  39. In this case probably a '\n'.  It leaves the unused char in the buffer,
  40. converts the read chars, and stores the value in j.
  41.  
  42. >
  43. >        printf("Enter 10 strings:\n");
  44. >        for(i=0; i<10; i++)             /* get 10 strings */
  45. >                gets(str[i]);           /* but can input 9 strings */
  46.  
  47. gets reads any chars in the input buffer for stdin (reading, but not 
  48. storing any '\n').  On the first iteration of the loop, it will read
  49. what was left in the buffer by the earlier scanf (there must at least
  50. be a '\n' in the buffer). Then it will begin reading your input lines.
  51.  
  52. To see this either watch your str[0] while stepping through in a debugger
  53. or add a printf( "Input string %d is: %s\n", i, str[i]); in your for loop.
  54.  
  55. BTW, you should use fgets( str[i], sizeof(str[0]), stdin) instead of your
  56. gets statement.  It will protect your array bounds.
  57.  
  58. A common solution to the scanf problem is to ise fgets to input a line
  59. from the user, then sscanf, atoi, etc to parse it.
  60.  
  61. >
  62. >        if(j>0 && j<10)
  63. >                printf("Answer: %s\n", str[j]);
  64. >}
  65.  
  66. -- 
  67. John R Buchan           -:|:-     Looking for that elusive FAQ?  ftp to:
  68. jatmon@mail.iag.net     -:|:-     rtfm.mit.edu /pub/usenet-by-group/....
  69.  
  70.